﻿using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Abp.Localization;
using Abp.Localization.Dictionaries;
using Abp.Localization.Dictionaries.Xml;
using Abp.Modules;
using Abp.Web.Mvc;
using Abp.Runtime.Caching.Redis;
using Abp.Hangfire.Configuration;
using Abp.Hangfire;
using Hangfire.Redis;
using Hangfire;
using System.Configuration;
using System;
using AVIS.OutBound.ORFleets.Cache;

namespace AVIS.OutBound.Web
{
    [DependsOn(
        typeof(AbpWebMvcModule),
        typeof(AbpRedisCacheModule),
        typeof(AbpHangfireModule),
        typeof(OutBoundDataModule),
        typeof(OutBoundApplicationModule),
        typeof(OutBoundWebApiModule))]
    public class OutBoundWebModule : AbpModule
    {
        public override void PreInitialize()
        {
            ////Add/remove languages for your application
            //Configuration.Localization.Languages.Add(new LanguageInfo("en", "English", "famfamfam-flag-england", true));
            //Configuration.Localization.Languages.Add(new LanguageInfo("tr", "Türkçe", "famfamfam-flag-tr"));
            //Configuration.Localization.Languages.Add(new LanguageInfo("zh-CN", "简体中文", "famfamfam-flag-cn"));
            //Configuration.Localization.Languages.Add(new LanguageInfo("ja", "日本語", "famfamfam-flag-jp"));

            ////Add/remove localization sources here
            //Configuration.Localization.Sources.Add(
            //    new DictionaryBasedLocalizationSource(
            //        OutBoundConsts.LocalizationSourceName,
            //        new XmlFileLocalizationDictionaryProvider(
            //            HttpContext.Current.Server.MapPath("~/Localization/OutBound")
            //            )
            //        )
            //    );

            ////Configure navigation/menu
            //Configuration.Navigation.Providers.Add<OutBoundNavigationProvider>();

            // use HangFire as background job manager.
            Configuration.BackgroundJobs.UseHangfire(configuration =>
            {
                var connectionString = ConfigurationManager.ConnectionStrings["Hangfire.Redis"].ConnectionString;
                configuration.GlobalConfiguration.UseRedisStorage(connectionString, new RedisStorageOptions
                {
                    Db = 2,
                });
            });

            // 你可以为该web应用的所有实例关闭工作执行，然后创建一个会执行后台工作的独立应用（比如一个Windows服务）。
            // http://www.cnblogs.com/farb/p/ABPBackgroundJobsAndWorkers.html
            Configuration.BackgroundJobs.IsJobExecutionEnabled = false;

            // 配置使用Redis缓存
            //Configuration.Caching.UseRedis();

            // 配置所有缓存时间为半小时
            Configuration.Caching.ConfigureAll(cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromMinutes(30);
            });

            // 配置车辆信息（ORFleet）的缓存时间为一天
            Configuration.Caching.Configure(ORFleetCacheItem.CacheName, cache =>
            {
                cache.DefaultSlidingExpireTime = TimeSpan.FromDays(1);
            });
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}
